﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Plexi.Science
{
	/// <summary>An array of rock types with their spawn probabilities for a biome, with a general rock spawn density percentage for the area of that biome.</summary>
	public class BiomeRockOccurrences
	{
		/// <summary>Default is 100, otherwise fewer rocks will spawn in this biome.</summary>
		public int RockSpawnDensity { get; set; }
		/// <summary>The probability of occurrence for certain rock types in this biome</summary>
		public RockTypePercent[] TypeOccurrences { get; set; }

		public RockTypePercent Choose(int typeSelector)
		{
			foreach (RockTypePercent rtp in TypeOccurrences)
			{
				if (typeSelector < rtp.Percent)
					return rtp;
				typeSelector -= rtp.Percent;
			}
			throw new ApplicationException(string.Format("SurfaceRocks: ERROR: failed to choose RockTypePercent from BiomeRockOccurrences! typeSelector: {0}, RockSpawnDensity: {1}, TypeOccurrences: {2}",
				typeSelector,
				RockSpawnDensity,
				(TypeOccurrences.Length == 0 ? "none!" :
				(string.Join(" | ", TypeOccurrences.Select(to => string.Format("{0} {1}", to.Type.Name, to.Percent)).ToArray()))
				)));
		}
	}
}
